using System;
using UnityEngine;

namespace NodeCanvas.BehaviourTrees
{
	[AddComponentMenu("")]
	public class BehaviourTree : Graph
	{
		public bool runForever = true;

		public float updateInterval;

		private float intervalCounter;

		private Status _rootStatus = Status.Resting;

		public Status rootStatus
		{
			get
			{
				return _rootStatus;
			}
			private set
			{
				_rootStatus = value;
			}
		}

		public override Type baseNodeType
		{
			get
			{
				return typeof(BTNodeBase);
			}
		}

		protected override void OnGraphStarted()
		{
			intervalCounter = updateInterval;
			rootStatus = base.primeNode.status;
		}

		protected override void OnGraphUpdate()
		{
			if (intervalCounter >= updateInterval)
			{
				intervalCounter = 0f;
				if (Tick(base.agent, base.blackboard) != Status.Running && !runForever)
				{
					StopGraph();
				}
			}
			intervalCounter += Time.deltaTime;
		}

		public Status Tick(Component agent, Blackboard blackboard)
		{
			if (rootStatus != Status.Running)
			{
				base.primeNode.ResetNode();
			}
			rootStatus = base.primeNode.Execute(agent, blackboard);
			return rootStatus;
		}
	}
}
